/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */

 //还是和前面的求和为k的子数组的个数的思想一样
class Solution {
public:
    unordered_map<long long,int> hash;
    int dfs(TreeNode* root,long long cur,int targetSum)
    {
        if(!root) return 0;
        int ret=0;//满足条件的路径的个数
        cur+=root->val;//路径的总长度

        if(hash.count(cur-targetSum))
        {
            ret=hash[cur-targetSum];
        }
        hash[cur]++;
        ret+=dfs(root->left,cur,targetSum);
        ret+=dfs(root->right,cur,targetSum);
        hash[cur]--;//回溯这一步非常关键！！！！
        return ret;
    }
    int pathSum(TreeNode* root, int targetSum) {
        hash[0]=1;
        return dfs(root,0,targetSum);

    }
};
